package com.media.themovieworld.screen.main;

import java.util.ArrayList;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.media.themovieworld.R;
import com.media.themovieworld.activity.MovieDetailActivity;
import com.media.themovieworld.api.ApiParams;
import com.media.themovieworld.api.RequestModel;
import com.media.themovieworld.common.fragment.MovieTaskScreen;
import com.media.themovieworld.model.MovieItemModel;
import com.media.themovieworld.model.ObServerListener;
import com.media.themovieworld.player.FilmPartManage;
import com.media.themovieworld.util.AppSettingPrefference;
import com.media.themovieworld.util.MovieImageLoader;

public class SpotLightScreen extends MovieTaskScreen implements ObServerListener{

	private ImageView imgView1;
	private TextView tv1;
	private ImageView imgView2;
	private TextView tv2;
	private ImageView imgView3;
	private TextView tv3;
	private ImageView imgView4;
	private TextView tv4;
	private ImageView imgView5;
	private TextView tv5;
	private ImageView imgView6;
	private TextView tv6;
	private ImageView imgView7;
	private TextView tv7;
	private ImageView imgView8;
	private TextView tv8;
	private ImageView imgView9;
	private TextView tv9;
	ArrayList<MovieItemModel> mListDatas;
	
	@Override
	protected View onCreateContentView(LayoutInflater inflater,
			ViewGroup container) {
		View v = inflater.inflate(R.layout.tmw__promote_film_screen, null);
		imgView1 = (ImageView) v.findViewById(R.id.tmw__promote_film_activity_image_1);
		imgView2 = (ImageView) v.findViewById(R.id.tmw__promote_film_activity_image_2);
		imgView3 = (ImageView) v.findViewById(R.id.tmw__promote_film_activity_image_3);
		imgView4 = (ImageView) v.findViewById(R.id.tmw__promote_film_activity_image_4);
		imgView5 = (ImageView) v.findViewById(R.id.tmw__promote_film_activity_image_5);
		imgView6 = (ImageView) v.findViewById(R.id.tmw__promote_film_activity_image_6);
		imgView7 = (ImageView) v.findViewById(R.id.tmw__promote_film_activity_image_7);
		imgView8 = (ImageView) v.findViewById(R.id.tmw__promote_film_activity_image_8);
		imgView9 = (ImageView) v.findViewById(R.id.tmw__promote_film_activity_image_9);
		
		tv1 = (TextView) v.findViewById(R.id.tmw__promote_film_activity_textview_1);
		tv2 = (TextView) v.findViewById(R.id.tmw__promote_film_activity_textview_2);
		tv3 = (TextView) v.findViewById(R.id.tmw__promote_film_activity_textview_3);
		tv4 = (TextView) v.findViewById(R.id.tmw__promote_film_activity_textview_4);
		tv5 = (TextView) v.findViewById(R.id.tmw__promote_film_activity_textview_5);
		tv6 = (TextView) v.findViewById(R.id.tmw__promote_film_activity_textview_6);
		tv7 = (TextView) v.findViewById(R.id.tmw__promote_film_activity_textview_7);
		tv8 = (TextView) v.findViewById(R.id.tmw__promote_film_activity_textview_8);
		tv9 = (TextView) v.findViewById(R.id.tmw__promote_film_activity_textview_9);
		
		loadData();
		return v;
	}
	
	private void loadData(){
		RequestModel requestData = ApiParams.getFilmParams("", "0", 0,2, 0,9, AppSettingPrefference.getInstance().getMovieLanguage());
//		SoapRequestModel requestData = MovieWorldApi.getFilmRequest(getActivity(), "", "0", 0,2, 0,9);
		executeSoapTaskReturnList(requestData, true, MovieItemModel.class, new MovieTaskListener() {
			
			@Override
			public void onTaskComplete(Object object) {
				if (object!=null) {
					removeRetryView();
					mListDatas = (ArrayList<MovieItemModel>) object;
					createUI();
				}else{
					addRetryView();
				}
			}
		});
	}

	@Override
	protected void onRetry() {
		removeRetryView();
		loadData();
	}
	
	public void createUI(){
		if (mListDatas == null) return;
		for (int i = 0; i < mListDatas.size(); i++) {
			MovieItemModel model = mListDatas.get(i);
			switch (i) {
			case 0:
				displayData(imgView1, tv1, model);
				break;
			case 1:
				displayData(imgView2, tv2, model);
				break;
			case 2:
				displayData(imgView3, tv3, model);
				break;
			case 3:
				displayData(imgView4, tv4, model);
				break;
			case 4:
				displayData(imgView5, tv5, model);
				break;
			case 5:
				displayData(imgView6, tv6, model);
				break;
			case 6:
				displayData(imgView7, tv7, model);
				break;
			case 7:
				displayData(imgView8, tv8, model);
				break;
			case 8:
				displayData(imgView9, tv9, model);
				break;
			default:
				break;
			}
		}
	}
	
	private void displayData(ImageView img,TextView tv,final MovieItemModel model){
		tv.setText(model.name);
		int imageWidth = img.getLayoutParams().width;
		int imageHeight = img.getLayoutParams().height;
		String url = ApiParams.getDownloadImageLink(imageWidth, imageHeight, model.icon);
		MovieImageLoader.getInstance().displayImage(url, img, MovieImageLoader.getDisplayImageOptionDefault());
		img.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				MovieItemModel mMovieData = model;
				FilmPartManage.getInstance().setCurrentFilmData(mMovieData);
				if (mMovieData==null ||mMovieData.filmPartList == null) {
					FilmPartManage.getInstance().setCurrentFilmPart(null);
				}else{
					FilmPartManage.getInstance().setCurrentFilmPart(mMovieData.filmPartList);
				}
//				MovieDetailScreen fragDetail = new MovieDetailScreen();
//				getMainActivity().switchContent(fragDetail, true);
				getMainActivity().startActivity(new Intent(getActivity(), MovieDetailActivity.class));
			}
		});
	}

	@Override
	public void update(Object ob) {
		onRetry();
	}
}
